<?php


require "database.php";


$base=new Db;
$ids=$_POST["ids"];
if($ids){
    
    
    $ids=json_decode($ids,1);
    
    for ($i = 0; $i < count($ids); $i++) {
        $base->doSql('delete from ey_arctype where id="'.$ids[$i].'"');
    }
}

$array=$base->doSql('select * from ey_arctype where status=1 and lang="cn"');



function getTree($array, $pid =0, $level = 0){
    //声明静态数组,避免递归调用时,多次声明导致数组覆盖
    static $list = [];
    foreach ($array as $key => $value){
      //第一次遍历,找到父节点为根节点的节点 也就是pid=0的节点
      if ($value['parent_id'] == $pid){
        //父节点为根节点的节点,级别为0，也就是第一级
        $value['level'] = $level;
        //把数组放到list中
        $list[] = $value;
        //把这个节点从数组中移除,减少后续递归消耗
        unset($array[$key]);
        //开始递归,查找父ID为该节点ID的节点,级别则为原级别+1
        getTree($array, $value['id'], $level+1);
      }
    }
    return $list;
}
/*
* 获得递归完的数据,遍历生成分类
*/
$array = getTree($array);
foreach($array as $key=> $value){
    
    
    
        $array[$key]["typename"]=str_repeat('|___', $value['level']). $value['typename'];
    
    
    if($array[$key]['level']<2){
        $array[$key]["class"]="trSelected";
    }else{
        $array[$key]["class"]="";
    }
}

return json($array);
